package hang.study.uistudy;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import junit.framework.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hang on 2015-10-6.
 */
public class TestSimpleListViewActivity extends Activity {

    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return 40;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LinearLayout line = new LinearLayout(TestSimpleListViewActivity.this);
            line.setOrientation(LinearLayout.HORIZONTAL);
            ImageView image=new ImageView(TestSimpleListViewActivity.this);
            image.setImageResource(R.drawable.b1);
            image.setMaxWidth(50);
            image.setMaxHeight(50);

            TextView text = new TextView(TestSimpleListViewActivity.this);
            text.setText("第"+(position+1)+"个列表项");
            text.setTextSize(20);
            text.setTextColor(Color.RED);

            line.addView(image);
            line.addView(text);

            return line;
        }
    };
    private String[] names = new String[]{
            "dsfd","r45h","fghj","罪过","奢侈","不需要"
    };
    private String[] descs = new String[]{
        "考六级大风蓝色","美丽的说服力","‘阚凯力","快来领风骚的","咖啡馆里","看了看lewd浪费"
    };
    private int[] imgIds = new int[]{
            R.drawable.a1,
            R.drawable.a2,
            R.drawable.a3,
            R.drawable.a4,
            R.drawable.a5,
            R.drawable.a6
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test_simple_adapter_layout);
        List<Map<String,Object>> listItems= new ArrayList<>();

        for(int i=0; i < names.length; i++){
            Map<String,Object> item = new HashMap<>();
            item.put("header",imgIds[i]);
            item.put("personName",names[i]);
            item.put("desc",descs[i]);

            listItems.add(item);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems,
                R.layout.test_simple_adapter_item,
                new String[]{"header","personName","desc"},
                new int[]{R.id.header,R.id.name,R.id.desc});

        ListView listView = (ListView)findViewById(R.id.myList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println(names[position]+"被选中了");
            }
        });
    }

}
